// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <pollux/functions/macros.h>

namespace kumo::pollux::functions::sparksql {
    template<typename T>
    struct BitwiseAndFunction {
        template<typename TInput>
        MELON_ALWAYS_INLINE void call(TInput &result, TInput a, TInput b) {
            result = a & b;
        }
    };

    template<typename T>
    struct BitwiseOrFunction {
        template<typename TInput>
        MELON_ALWAYS_INLINE void call(TInput &result, TInput a, TInput b) {
            result = a | b;
        }
    };

    template<typename T>
    struct BitwiseXorFunction {
        template<typename TInput>
        MELON_ALWAYS_INLINE void call(TInput &result, TInput a, TInput b) {
            result = a ^ b;
        }
    };

    template<typename T>
    struct BitwiseNotFunction {
        template<typename TInput>
        MELON_ALWAYS_INLINE void call(TInput &result, TInput a) {
            result = ~a;
        }
    };

    template<typename T>
    struct ShiftLeftFunction {
        template<typename TInput1, typename TInput2>
        MELON_ALWAYS_INLINE void call(TInput1 &result, TInput1 a, TInput2 b) {
            if constexpr (std::is_same_v<TInput1, int32_t>) {
                if (b < 0) {
                    b = b % 32 + 32;
                }
                if (b >= 32) {
                    b = b % 32;
                }
            }
            if constexpr (std::is_same_v<TInput1, int64_t>) {
                if (b < 0) {
                    b = b % 64 + 64;
                }
                if (b >= 64) {
                    b = b % 64;
                }
            }
            result = a << b;
        }
    };

    template<typename T>
    struct ShiftRightFunction {
        template<typename TInput1, typename TInput2>
        MELON_ALWAYS_INLINE void call(TInput1 &result, TInput1 a, TInput2 b) {
            if constexpr (std::is_same_v<TInput1, int32_t>) {
                if (b < 0) {
                    b = b % 32 + 32;
                }
                if (b >= 32) {
                    b = b % 32;
                }
            }
            if constexpr (std::is_same_v<TInput1, int64_t>) {
                if (b < 0) {
                    b = b % 64 + 64;
                }
                if (b >= 64) {
                    b = b % 64;
                }
            }
            result = a >> b;
        }
    };

    template<typename T>
    struct BitCountFunction {
        template<typename TInput>
        MELON_ALWAYS_INLINE void call(int32_t &result, TInput num) {
            constexpr int kMaxBits = sizeof(TInput) * CHAR_BIT;
            auto value = static_cast<uint64_t>(num);
            result = bits::countBits(&value, 0, kMaxBits);
        }
    };

    template<typename T>
    struct BitGetFunction {
        template<typename TInput>
        MELON_ALWAYS_INLINE void call(int8_t &result, TInput num, int32_t pos) {
            constexpr int kMaxBits = sizeof(TInput) * CHAR_BIT;
            POLLUX_USER_CHECK_GE(
                pos,
                0,
                "The value of 'pos' argument must be greater than or equal to zero.");
            POLLUX_USER_CHECK_LT(
                pos,
                kMaxBits,
                "The value of 'pos' argument must not exceed the number of bits in 'x' - 1.");
            result = (num >> pos) & 1;
        }
    };
} // namespace kumo::pollux::functions::sparksql
